<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="部门名称">
                  <el-input v-clearEmoji v-model.trim="deptName" style="width: 95%" placeholder="部门名称" clearable
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="更新时间">
                  <el-date-picker clearable v-model="date" type="daterange" style="width: 95%" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" v-has="'dep_add'" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1" :row-config="{ isHover: true }"
          :scroll-y="{ enabled: true, gt: 21 }" align="center" style="width: 100%" height="100%" size="medium">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="deptName" title="部门名称" min-width="200"/>
          <vxe-column field="deptNum" title="成员数量" min-width="100"/>
          <vxe-column field="description" title="部门描述" min-width="200"/>
          <vxe-column field="deviceId" title="更新时间" min-width="150">
            <template v-slot="{ row }">
              {{ row.updateTime | parseTime }}
            </template>
          </vxe-column>
          <vxe-column v-if="$showFn(['dep_detail', 'dep_edit', 'dep_del'])" title="操作" fixed="right" width="200">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)" @click.stop="getDetail(row)"
                v-has="'dep_detail'">详情</el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255)" type="text" @click.stop="edit(row)"
                v-has="'dep_edit'">修改</el-button>
              <el-button type="text" style="margin-right: 10px; color: red" @click="$delFn('部门', del, row)"
                v-has="'dep_del'">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :title="title" @btnOk="btnOk" @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="100px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-clearEmoji v-model.trim="addForm.deptName" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="leader">
          <el-input v-clearEmoji v-model.trim="addForm.leader" />
        </el-form-item>
        <el-form-item label="部门描述" prop="description">
          <el-input v-clearEmoji v-model.trim="addForm.description" type="textarea" maxlength="100" show-word-limit />
        </el-form-item>
        <el-form-item label="部门管理员">
          <el-select v-model="addForm.userIdList" filterable multiple popper-class="eloption"
            :placeholder="title === '详情' ? '' : '请选择'">
            <el-option v-for="user in userList" :key="user.id" :value="user.id" :label="user.employeeName" />
          </el-select>
        </el-form-item>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import AddDialog from "@/components/Goods/AddDialog";
import {
  getDeptList,
  addDept,
  getUserList,
  updateDeptDetail,
  delDept,
  serDept,
} from "@/api/system";
export default {
  name: "Department",
  components: {
    AddDialog,
  },
  data() {
    return {
      loading: false,
      width: "50%",
      title: "新增",
      showDialog: false,
      isSearch: false,
      addForm: {
        deptName: "", // 部门名称
        leader: "", // 负责人
        description: "", // 部门描述
        userIdList: [], //	部门管理员
      },
      addRules: {
        deptName: [
          {
            required: true,
            message: "请输入部门名称",
            trigger: "change",
          },
          {
            min: 1,
            max: 15,
            message: "长度在1到15个字符",
            trigger: "change",
          },
          {
            pattern:
              /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/,
            message: "只能输入中文",
            trigger: "change",
          },
        ],
        leader: [
          {
            required: true,
            message: "请输入部门负责人",
            trigger: "change",
          },
          {
            min: 1,
            max: 15,
            message: "长度在1到15个字符",
            trigger: "change",
          },
          {
            pattern:
              /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/,
            message: "只能输入中文",
            trigger: "change",
          },
        ],
        description: [
          {
            required: true,
            message: "请输入部门描述",
            trigger: "change",
          },
          {
            min: 1,
            max: 100,
            message: "长度在1到100个字符",
            trigger: "change",
          },
          {
            pattern:
              /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/,
            message: "只能输入中文",
            trigger: "change",
          },
        ],
      },
      deptList: [],
      deptName: "",
      date: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      userList: [],
      posId: "",
    };
  },
  created() {
    this.getDeptList();
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const res = await getUserList();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        // console.log('deptList', result)
        this.userList = result.sysUserList;
        console.log("userList", this.userList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getDeptList() {
      const serData = {
        deptName: this.deptName ? this.deptName : "",
        openTime: this.date?.length ? this.date[0] : "",
        endTime: this.date?.length ? this.date[1] : "",
      };
      if (!this.isSearch) {
        const serData = {};
      }
      const res = await getDeptList({ ...serData, ...this.page });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.deptList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("deptList", this.deptList);
        const $table = this.$refs.xTable1
        $table.loadData(this.deptList);
      } else {
        // this.$message.error(res.message);
      }
    },
    addFn() {
      this.getUserList();
      this.title = "新增";
      this.showDialog = true;
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getDeptList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;

      this.getDeptList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        deptName: "", // 部门名称
        leader: "", // 负责人
        description: "", // 部门描述
        userIdList: [], //	部门管理员
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === "新增") {
        // 新增
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            //
            const res = await addDept(this.addForm);
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.getDeptList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "详情") {
        // 关闭
        this.close();
      } else if (this.title === "修改") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await updateDeptDetail({
              ...this.addForm,
              id: this.posId,
            });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getDeptList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    async getDetail(row) {
      this.getUserList();
      this.showDialog = true;
      this.title = "详情";
      console.log(row);
      this.addForm.deptName = row.deptName;
      this.addForm.leader = row.leader;
      this.addForm.description = row.description;
      this.addForm.userIdList = row.userIdList;
    },
    async edit(row) {
      this.getUserList();
      this.showDialog = true;
      this.title = "修改";
      console.log(row);
      this.addForm.deptName = row.deptName;
      this.addForm.leader = row.leader;
      this.addForm.description = row.description;
      this.addForm.userIdList = row.userIdList;
      this.posId = row.id;
    },
    async del(row) {
      const res = await delDept({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.deptList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getDeptList();
      } else {
        // this.$message.error(res.message);
      }
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getDeptList();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.deptName = "";
      this.date = [];
      this.getDeptList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
